/*
 * 描述： <描述>
 * 修改人： rain
 * 修改时间： 2016年4月5日
 * 项目： rainhy-core
 */
package com.rainhy.core.test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;

import com.rainhy.core.exception.RainhyException;

/**
 * <功能简述><br>
 * <功能详细描述>
 * 
 * @author rain
 * @version [版本号, 2016年4月5日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class BlueSky {
    
    private static CookieStore cookieStore = new BasicCookieStore();
    
    private static CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    
    public static void main(String[] args) {
        String loginUrl = "http://ww.88sxy.com/Helper/CheckUserlogin.asp?q=aHsvLS3BL85De0veWmCAZIjCLi5BL84yWkzde9gSRGz3YVCBYW4yWkzde8XFXHTHXS9BMEXDM89FXVL=";
        String loginResonpse = sendGetMethod(loginUrl, null);
        //        System.out.println(loginResonpse);
        
        BasicClientCookie cookie = new BasicClientCookie("CollectManage_ShowMenu", "1");
        cookie.setVersion(0);
        cookie.setDomain("ww.88sxy.com");
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
        String str1 = "http://ww.88sxy.com/Helper/Interface.asp?q=TlUgY9YraIcyWkzde86yWkzde1zqdS9DL85BLmCdWGvyL2CdWGvyXS3FLS4lXSXCYVPEMVLDYVTCY19uLEPuN14kY8fvXV9=";
        Map<String, String> header = new HashMap<>();
        header.put("Accept", "*/*");
        loginResonpse = sendGetMethod(str1, header);
        System.out.println(loginResonpse);
        
        //        String str2 = "ww.88sxy.com/Helper/Interface.asp?q=QlCAcGMGXHMkbIMyWkzde8PHLS5CLSfIe0veWmCAZIjCLi5BL84yWkzde86yWkzde19DM89DYl9HLVUjLEUiLlUkLVQgN8LFN8ghYVPuNV0g";
        //        loginResonpse = sendGetMethod(str1, header);
        //        System.out.println(loginResonpse);
        
    }
    
    /**
     * 
     * 发送 GET 请求
     *
     * @param url
     * @param header
     * @return
     * 
     * @return String [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     * @version [版本号, 2016年4月5日]
     * @author rain
     */
    public static String sendGetMethod(String url, Map<String, String> header) {
        CloseableHttpResponse response = null;
        try {
            HttpGet httpGet = new HttpGet(url);
            if (MapUtils.isNotEmpty(header)) {
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    httpGet.setHeader(entry.getKey(), entry.getValue());
                }
            }
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            throw new RainhyException(e, "http GET error");
        } finally {
            IOUtils.closeQuietly(response);
            //            IOUtils.closeQuietly(client);
        }
    }
}
